package xiaomogu.task_engine.admin.core.route;

import xiaomogu.task_engine.admin.core.route.strategy.*;


public enum RouteStrategy {

    FIRST("first", new First()),
    LAST("last", new Last()),
    ROUND("round", new Round()),
    RANDOM("random", new Random()),
    CONSISTENT_HASH("consistent hash", new ConsistentHash()),
    LEAST_FREQUENTLY_USED("lfu", new LFU()),
    LEAST_RECENTLY_USED("lru", new LRU()),
    FAILOVER("failover", new RouteFailover()),
    BUSYOVER("busyover", new Busyover()),
    SHARDING_BROADCAST("shard", null);

    private String code;
    private AddressRoute addressRoute;

    RouteStrategy(String code, AddressRoute addressRoute) {
        this.code = code;
        this.addressRoute = addressRoute;
    }

    public static RouteStrategy match(String code, RouteStrategy defaultItem) {
        if (code != null) {
            for (RouteStrategy routeStrategy : RouteStrategy.values()) {
                if (routeStrategy.name().equals(code)) {
                    return routeStrategy;
                }
            }
        }
        return defaultItem;
    }

    public String getCode() {
        return code;
    }

    public AddressRoute getAddressRoute() {
        return addressRoute;
    }

}
